<?php
class item_basicsteal extends template_item
{
	var $default_name = "Steal Money";
	var $default_description = "Use this item to steal money from people. Fail and you lose twice the amount you tried.";
	
	var $allow_multiple = 1;
	
	var $item_config = array();
	var $description = "";
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item['item_name'] = str_replace(array('{low}','{high}'),array($this->item_config['amount_min'],$this->item_config['amount_max']),$this->item['item_name']);
			
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['protected'] = array($this->ipsclass->vars['admin_group']);
			$this->item_config['amount_min'] = 0;
			$this->item_config['amount_max'] = 10000;
			$this->item_config['percent_min'] = 100;
			$this->item_config['percent_max'] = 50;
		}
		
		$this->description = str_replace(array('{low}','{high}'),array($this->item_config['amount_min'],$this->item_config['amount_max']),$this->item['item_description']);
	}
	
	function admin_form()
	{
		$groups = array();
		foreach($this->ipsclass->cache['group_cache'] as $g_id => $group)
		{
			$groups[] = array($g_id,$group['g_title']);
		}
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"Tags for item name and description",
															"{low} = The minimum amount can be stolen<br />{hight} = The maximum amount of money can be stolen<br />Both are settable below.",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Which groups are protected from this item?</b>",
															$this->ipsclass->adskin->form_multiselect("item[".$this->item['item_id']."][protected][]", $groups, $this->item_config['protected']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Minimum amount to steal</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][amount_min]", $this->item_config['amount_min']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Percent of a successful steal when stealing the minimum amount</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][percent_min]", $this->item_config['percent_min']).'%',
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Maximum amount to steal</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][amount_max]", $this->item_config['amount_max']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Percent of a successful steal when stealing the maximum amount</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][percent_max]", $this->item_config['percent_max']).'%',
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['protected'] = $input['protected'] ? $input['protected'] : array();
		$save['amount_min'] = $this->ipsclass->numbers->parse_input_number($input['amount_min'],'int');
		$save['amount_max'] = $this->ipsclass->numbers->parse_input_number($input['amount_max'],'int');
		$save['percent_min'] = $this->ipsclass->numbers->parse_input_number($input['percent_min']);
		$save['percent_max'] = $this->ipsclass->numbers->parse_input_number($input['percent_max']);
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
		return <<<EOF
<script type="text/javascript" src='jscripts/ipb_xhr_findnames.js'></script>
<div id='ipb-get-members' style='border:1px solid #000; background:#FFF; padding:2px;position:absolute;width:210px;display:none;z-index:1'></div>
<div class='formsubtitle'>{$this->ipsclass->lang['steal_money']}</div>
<form action='{$this->base_url}' method='post' id='changeform'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['howmuchtosteal']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='input' name='amount' value='0' size='30' class='forminput' />
			</td>
		</tr>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['which_member']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='input' id='member_name' name='member_name' value='' size='30' class='forminput' autocomplete='off' style='width:210px' />
			</td>
		</tr>
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['steal']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
<script type="text/javascript">
	// INIT find names
	init_js( 'changeform', 'member_name');
	// Run main loop
	setTimeout( 'main_loop()', 10 );
</script>
EOF;
	}
	
	function input_done()
	{
		return isset($this->ipsclass->input['member_name']) && isset($this->ipsclass->input['amount']) && isset($this->ipsclass->input['item_used']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"members_l_display_name='".strtolower($this->ipsclass->input['member_name'])."'"));
		if(!$member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'no_user'));
		}
		
		if(in_array($member['mgroup'],$this->item_config['protected']))
		{
			$this->ipsclass->Error(array('MSG'=>'protected_user'));
		}
		
		$this->ipsclass->input['amount'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['amount'],'int');
		
		if($this->ipsclass->numbers->compare($this->ipsclass->input['amount'],$this->item_config['amount_min']) < 0 || $this->ipsclass->numbers->compare($this->ipsclass->input['amount'],$this->item_config['amount_max']) > 0)
		{
			$this->ipsclass->lang['cant_steal_that_much'] = str_replace("<#LOW#>", $this->item_config['amount_min'],$this->ipsclass->lang['cant_steal_that_much']);
			$this->ipsclass->lang['cant_steal_that_much'] = str_replace("<#HIGH#>", $this->item_config['amount_max'],$this->ipsclass->lang['cant_steal_that_much']);
			$this->ipsclass->Error(array('MSG'=>'cant_steal_that_much'));
		}
		if($this->ipsclass->numbers->compare($this->ipsclass->input['amount'],$member[$this->ipsclass->vars['market_members_field']]) > 0)
		{
			$this->ipsclass->Error(array('MSG'=>'member_dont_have_that_much'));
		}
		
		$difference = $this->ipsclass->numbers->random_number(0,100,1);
		$delta_p = $this->ipsclass->numbers->subtract($this->item_config['percent_max'],$this->item_config['percent_min']);
		$delta_a = $this->ipsclass->numbers->subtract($this->item_config['amount_max'],$this->item_config['amount_min']);
		$compare = $this->ipsclass->numbers->add($this->ipsclass->numbers->divide($this->ipsclass->numbers->multiply($this->ipsclass->numbers->subtract($this->ipsclass->input['amount'],$this->item_config['amount_min']),$delta_p),$delta_a),$this->item_config['percent_min']);
		
		$lang = '';
		$inv_id = reset($inventory_ids);
		// Success!
		if($this->ipsclass->numbers->compare($compare,$difference) > 0)
		{
			$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->input['amount']),
					),"id=".$this->ipsclass->member['id']);
			$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->subtract($member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->input['amount']),
									),"id=".$member['id']);
			
			$this->_write_log($inv_id, "Successfully stole {$this->ipsclass->input['amount']} from <a href='{$this->ipsclass->base_url}showuser={$member['id']}'>{$member['members_display_name']}</a>");
			
			$lang = 'steal_successful';
		}
		// Failed
		else
		{
			$this->ipsclass->DB->do_update('members',array(
															$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->subtract($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$this->ipsclass->numbers->multiply($this->ipsclass->input['amount'],2)),
					),"id=".$this->ipsclass->member['id']);
			
			$this->_write_log($inv_id, "Failed stealing {$this->ipsclass->input['amount']} from <a href='{$this->ipsclass->base_url}showuser={$member['id']}'>{$member['members_display_name']}</a>");
			
			$lang = 'steal_failed';
		}
		
		$this->delete_items($inventory_ids);
		
		$this->ipsclass->market_class->redirect($lang);
	}
	
	function parse_store_description()
	{
		return $this->description;
	}
	
	function parse_group_description()
	{
		return $this->description;
	}
	
	function parse_single_description()
	{
		return $this->description;
	}
}
?>